package action.report;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.Region;

import service.report.ReportDetailService;
import util.Time;
import util.Tool;

import core.action.BaseAction;
import exception.Exceptions;

@SuppressWarnings("deprecation")
public class ReportDetailAction extends BaseAction{
	private List<?> reportDetailList;
	
	private ReportDetailService reportDetailService;
	
	private int inforLoanid;   //查询详细信息需要的id
	
	@SuppressWarnings("rawtypes")
	private List listLoan;
	@SuppressWarnings("rawtypes")
	private List listBack;
	
	private String clientName;
	
	private String fileName;//导出文件
	
	public String execute(){
		reportDetailList = this.reportDetailService.getReportDetailList();
		return "success";
	}
	
	/**
	 * 查询借款人借款和放款的详细的信息
	 * */
	public String getLoanAndBackInfor() throws UnsupportedEncodingException{
		//clientName = new String(clientName.getBytes("ISO-8859-1"), "utf-8");//中文字符乱码，重新编码转码。已经废弃。如果tomcat中已经配置URIEncoding="UTF-8"，则需要废弃
		clientName = this.reportDetailService.getClientInforByLoanid(inforLoanid);
		this.listLoan = this.reportDetailService.getLoanInfor(inforLoanid);
		this.listBack = this.reportDetailService.getBackInfor(inforLoanid);
		return "infor";
	}
	
	/**
	 * 导出报表,自动调用getInputStream()
	 * */
	public String reportExport(){
		return "report";
	}
	
	
	public InputStream getInputStream() throws Exceptions {
		String dateTime = Time.Timestamp2TimeString(Time.CurrentTimestamp());
		reportDetailList = this.reportDetailService.getReportDetailList();
		HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFCellStyle style = workbook.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中  
		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中 
		
        HSSFSheet sheet = createSheet(workbook, style);
        addMapRow(sheet, 1, style);
        
		//下载文件名
        fileName=Tool.toChinese("贷款明细汇总表" + dateTime + ".xls");
        //输出下载
		ByteArrayOutputStream baos = new ByteArrayOutputStream();  
		try {
			workbook.write(baos);
		} catch (IOException e) {
			e.printStackTrace();
		}
        byte[] ba = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(ba);
        return bais;
	}
	
	public HSSFSheet createSheet(HSSFWorkbook workbook, HSSFCellStyle style){
		HSSFSheet sheet = workbook.createSheet("贷款明细汇总表");
		
		sheet.addMergedRegion(new Region(0, (short)0, 0, (short)15));
		
        //合并单元格
        HSSFRow header = sheet.createRow(0);
        HSSFCell cell = null;
        header = sheet.createRow(0);
        cell = header.createCell((short)0);
        cell.setCellValue(new HSSFRichTextString("贷款明细汇总表"));
        cell.setCellStyle(style);
        
        header = sheet.createRow(1);
        cell = header.createCell((short)0);
        cell.setCellValue(new HSSFRichTextString("借款人名称"));
        cell.setCellStyle(style);
        
        cell = header.createCell((short)1);
        cell.setCellValue(new HSSFRichTextString("借款日期"));
        cell.setCellStyle(style);
        
        cell = header.createCell((short)2);
        cell.setCellValue(new HSSFRichTextString("到期日期"));
        cell.setCellStyle(style);
        
        cell = header.createCell((short)3);
        cell.setCellValue(new HSSFRichTextString("借款金额"));
        cell.setCellStyle(style);
        
        cell = header.createCell((short)4);
        cell.setCellValue(new HSSFRichTextString("余额"));
        cell.setCellStyle(style);
        
        cell = header.createCell((short)5);
        cell.setCellValue(new HSSFRichTextString("期限"));
        cell.setCellStyle(style);
        
        cell = header.createCell((short)6);
        cell.setCellValue(new HSSFRichTextString("利率"));
        cell.setCellStyle(style);
        
        cell = header.createCell((short)7);
        cell.setCellValue(new HSSFRichTextString("还款日"));
        cell.setCellStyle(style);
        
        cell = header.createCell((short)8);
        cell.setCellValue(new HSSFRichTextString("用途"));
        cell.setCellStyle(style);
        
        cell = header.createCell((short)9);
        cell.setCellValue(new HSSFRichTextString("担保方式"));
        cell.setCellStyle(style);
        
        cell = header.createCell((short)10);
        cell.setCellValue(new HSSFRichTextString("涉农"));
        cell.setCellStyle(style);
        
        cell = header.createCell((short)11);
        cell.setCellValue(new HSSFRichTextString("涉农累计额"));
        cell.setCellStyle(style);
        
        cell = header.createCell((short)12);
        cell.setCellValue(new HSSFRichTextString("涉农金额"));
        cell.setCellStyle(style);
        
        cell = header.createCell((short)13);
        cell.setCellValue(new HSSFRichTextString("客户类型"));
        cell.setCellStyle(style);
        
        cell = header.createCell((short)14);
        cell.setCellValue(new HSSFRichTextString("累计额"));
        cell.setCellStyle(style);
        
        cell = header.createCell((short)15);
        cell.setCellValue(new HSSFRichTextString("余额"));
        cell.setCellStyle(style);
        return sheet;
	}
	
	@SuppressWarnings("rawtypes")
	public int addMapRow(HSSFSheet sheet, int rowNumber, HSSFCellStyle style){
		int listSize = reportDetailList.size();
		for(int i = 0; i < listSize; i++){
			HSSFRow row = sheet.createRow(i + 2);
			Map rowMap = (Map)reportDetailList.get(i);
			row.createCell((short)0).setCellValue(rowMap.get("customname").toString());
	        row.createCell((short)1).setCellValue(Time.DateString2Timestamp(rowMap.get("startdate").toString()));
	        row.createCell((short)2).setCellValue(Time.DateString2Timestamp(rowMap.get("enddate").toString()));
	        row.createCell((short)3).setCellValue(rowMap.get("applymoney").toString());
	        row.createCell((short)4).setCellValue(rowMap.get("balanceMoney").toString());
	        if(rowMap.get("applytime") != null){
	        	row.createCell((short)5).setCellValue(rowMap.get("applytime").toString() + rowMap.get("applytimetype").toString());
	        }
	        row.createCell((short)6).setCellValue(rowMap.get("performrates").toString());
	        if(rowMap.get("giveTime") != null){
	        	row.createCell((short)7).setCellValue(Time.DateString2Timestamp(rowMap.get("giveTime").toString()));
	        }
	        if(rowMap.get("loanuse") != null){
	        	row.createCell((short)8).setCellValue(rowMap.get("loanuse").toString());
	        }
	        if(rowMap.get("loanofmode") != null){
	        	row.createCell((short)9).setCellValue(rowMap.get("loanofmode").toString());
	        }
	        if(rowMap.get("isAboutN") != null){
	        	row.createCell((short)10).setCellValue(rowMap.get("isAboutN").toString());
	        }
	        row.createCell((short)11).setCellValue(rowMap.get("applyaboutMoney").toString());
	        row.createCell((short)12).setCellValue(rowMap.get("balanceaboutMoney").toString());
	        row.createCell((short)13).setCellValue(rowMap.get("clientType").toString());
	        row.createCell((short)14).setCellValue(rowMap.get("money").toString());
	        row.createCell((short)15).setCellValue(rowMap.get("bMoney").toString());
		}
		return rowNumber;
	}

	public List<?> getReportDetailList() {
		return reportDetailList;
	}

	public void setReportDetailList(List<?> reportDetailList) {
		this.reportDetailList = reportDetailList;
	}

	public ReportDetailService getReportDetailService() {
		return reportDetailService;
	}

	public void setReportDetailService(ReportDetailService reportDetailService) {
		this.reportDetailService = reportDetailService;
	}

	public int getInforLoanid() {
		return inforLoanid;
	}

	public void setInforLoanid(int inforLoanid) {
		this.inforLoanid = inforLoanid;
	}

	@SuppressWarnings("rawtypes")
	public List getListLoan() {
		return listLoan;
	}

	@SuppressWarnings("rawtypes")
	public void setListLoan(List listLoan) {
		this.listLoan = listLoan;
	}

	@SuppressWarnings("rawtypes")
	public List getListBack() {
		return listBack;
	}

	@SuppressWarnings("rawtypes")
	public void setListBack(List listBack) {
		this.listBack = listBack;
	}

	public String getClientName() {
		return clientName;
	}

	public void setClientName(String clientName) {
		this.clientName = clientName;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

}
